package com.zxl.placeholderParse;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CustomTemplateEngine{
    
    private Map<String, InputStream> map = new HashMap<String, InputStream>();
    
    private CustomTagParse ctp;
    
    public CustomTemplateEngine(CustomTagParse ctp) {
        super();
        this.ctp = ctp;
    }
    
    public String render(String templateName, Map<String, Object> mapData) throws IOException {
        InputStream is = map.get(templateName);
        if (is == null) {
            is = this.getClass().getClassLoader().getResourceAsStream(templateName);
            if (is == null) {
                return null;
            }
            map.put(templateName, is);
        }
        return ctp.parse(is, mapData);
    }
    
    public static void main(String[] args) throws IOException {
        CustomTagParse ctp = new CustomTagParse();
        CustomTemplateEngine cte = new CustomTemplateEngine(ctp);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("tradeNo", "B201606070000008");
        map.put("bookNo", "ADWWEG");
        map.put("payTotal", "270.0");
        List<TestDomain> testList = new ArrayList<TestDomain>();
        TestDomain td = new TestDomain("abc",15,"男");
        testList.add(td);
        map.put("testList", testList);
        System.out.println(cte.render("paysuccess2.html", map));
    }

}
